#!/bin/env python
# -*- coding:utf-8
"""
@author:xingz
@file:Tictactoe_ai.py
@time:2021/11/17
"""
from tictactoe.Tictactoe import TTTBoard, TTTPiece
from board import Move
from minmax import find_best_move

board = TTTBoard()


def get_player_move(board: TTTBoard) -> Move:
    player_move = Move(-1)
    while player_move not in board.legal_moves:
        try:
            legal_move = board.legal_moves
            play: int = int(input(f"Enter a legal square {legal_move}: "))
            player_move: Move = Move(play)
        except:
            pass
    return player_move


if __name__ == '__main__':
    while True:
        human_move = get_player_move(board)
        board = board.move(human_move)
        print("HUMAN->")
        print(board)
        if board.is_win:
            print("Human Win!!")
            break
        elif board.is_draw:
            print("Draw!!")
            break
        ai_move = find_best_move(board)
        board = board.move(ai_move)
        print("AI->")
        print(board)
        if board.is_win:
            print("AI Win!!")
            break
        elif board.is_draw:
            print("Draw!!")
            break
